package com.wxw.BaseLibrary.dialog;

import android.app.ProgressDialog;
import android.content.Context;
import com.wxw.BaseLibrary.tool.Timeout;

/**
 * Created by user on 2016/4/3.
 */
public class WaitingDialog {
    private static final String TITLE = "System Information";
    private ProgressDialog progressDialog;
    private Timeout timeout;

    private Timeout.OnTimeoutListener onTimeoutListener;

    public WaitingDialog(Context context, String message, int time, Timeout.OnTimeoutListener onTimeoutListener) {
        this(context, TITLE, message, time, onTimeoutListener);
    }

    public WaitingDialog(Context context, String title, String message, int time, Timeout.OnTimeoutListener onTimeoutListener) {
        this.onTimeoutListener = onTimeoutListener;
        progressDialog = ProgressDialog.show(context, title, message, true, false);
        timeout = new Timeout(time);
        timeout.setOnTimeoutListener(new Timeout.OnTimeoutListener() {
            @Override
            public void onTimeout(Object sender, int time) {
                if (progressDialog != null) {
                    progressDialog.dismiss();
                }
                if (WaitingDialog.this.onTimeoutListener != null) {
                    WaitingDialog.this.onTimeoutListener.onTimeout(WaitingDialog.this, time);
                }
            }
        });
        timeout.start();
    }

    public void stop() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
        if (timeout != null) {
            timeout.stop();
        }
    }
}
